package cemp.service.impl;

import cemp.domain.response.*;
import cemp.entity.StockKpiDay;
import cemp.entity.StockPlate;
import cemp.entity.StockPlateStocks;
import cemp.mapper.StockPlateMapper;
import cemp.mapper.StockPlateStocksMapper;
import cemp.service.StockPlateService;
import cemp.service.StockPlateStocksService;
import cemp.util.DateUtil;
import cemp.util.StockHttpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cemp.common.constant.StockCommonConstant.*;

@Slf4j
@Service
public class StockPlateStocksServiceImpl extends ServiceImpl<StockPlateStocksMapper, StockPlateStocks>
        implements StockPlateStocksService {

    @Autowired
    RestTemplate restTemplate;

    @Override
    public void insertPlateStocks(String plateCode) {
        try{
            //删除板块下所有股票
            this.deleteByPlateCode(plateCode);
            //查询板块下所有股票
            List<StockPlateStocks> results =  getByApi(plateCode);
            //批量添加
            //this.getBaseMapper().insert(results.get(0));
            if(results != null && results.size() > 0){
                this.getBaseMapper().insertBatchSomeColumn(results);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("123");
        }


    }

    public void deleteByPlateCode(String plateCode){
        LambdaQueryWrapper<StockPlateStocks> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StockPlateStocks::getPlatecode,plateCode);
        this.getBaseMapper().delete(lqw);
    }

    public List<StockPlateStocks> getByApi(String plateCode){
        String url = String.format("%s%s?token=%s&bkCode=%s&pageNo=1&pageSize=5000",STOCK_HOST,STOCK_PLATE_STOCKS,STOCK_TOKEN,plateCode);
        ResponseEntity<ApiCommonResponse<ApiPlateStocksResponse>> response = StockHttpUtil.doGet(restTemplate,
                url,
                HttpMethod.GET,
                new ParameterizedTypeReference<ApiCommonResponse<ApiPlateStocksResponse>>() {}
        );
        if(CollectionUtil.isNotEmpty(response.getBody().getData())){
            return response.getBody().getData().stream().map(apiPlateStocksResponse -> new StockPlateStocks(null ,plateCode, apiPlateStocksResponse.getF12())).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
